package org.fuys.coder.domain.act.lottery.service.strategy.impl;

import org.fuys.coder.common.entity.Pair;
import org.fuys.coder.domain.act.lottery.model.req.draw.DrawReq;
import org.fuys.coder.domain.act.lottery.model.req.strategy.StrategyAddReq;
import org.fuys.coder.domain.act.lottery.model.vo.strategy.LotteryStrategyVO;
import org.fuys.coder.domain.act.lottery.repository.ILotteryStrategyRepository;
import org.fuys.coder.domain.act.lottery.service.strategy.factory.LotteryStrategyFactory;
import org.fuys.coder.domain.act.lottery.model.vo.strategy.LotteryStrategyDetailVO;
import org.fuys.coder.domain.act.lottery.service.strategy.ILotteryStrategyService;
import org.fuys.coder.common.entity.PageDTO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.domain.lottery.service.strategy.impl
 * @className: LotteryStrategyServiceImpl
 * @author: WangJing
 * @description: 服务实现
 * @date: 2024/7/5 23:38
 * @version: 1.0
 */
@Service
public class LotteryStrategyServiceImpl implements ILotteryStrategyService {

    @Resource
    private ILotteryStrategyRepository lotteryStrategyRepository;

    @Override
    public List<Pair<LotteryStrategyVO, LotteryStrategyDetailVO>> getAllStrategyInfo() {
        return lotteryStrategyRepository.getAllStrategyInfo();
    }

    @Override
    @Transactional(rollbackForClassName = "Exception")
    public void addStrategy(StrategyAddReq req) {
        try {
            LotteryStrategyVO strategyVO=new LotteryStrategyVO();
            strategyVO.setStrategyDesc(req.getDesc());
            strategyVO.setStrategyMode(req.getMode());
            strategyVO.setSuitNum(req.getSuitNum());
            final Integer id = lotteryStrategyRepository.addStrategy(strategyVO);
            List<LotteryStrategyDetailVO> detailList=new ArrayList<>();
            List<Pair<Integer, BigDecimal>> list=new ArrayList<>();
            for (int i = 0; i < req.getSuitNum(); i++) {
                LotteryStrategyDetailVO detailVO=new LotteryStrategyDetailVO();
                detailVO.setPosition(i+1);
                final BigDecimal weight = req.getOdds().get(i);
                detailVO.setWeight(weight);
                detailVO.setStrategyId(id);
                Pair<Integer,BigDecimal> pair=new Pair<>(i+1, weight);
                detailList.add(detailVO);
            }
            lotteryStrategyRepository.addDetails(detailList);
            LotteryStrategyFactory.addTypeOddsInfo(new Pair<>(req.getMode(),req.getSuitNum()),list);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public List<Pair<LotteryStrategyVO, LotteryStrategyDetailVO>> getPageStrategyInfo(PageDTO pageDTO) {
        return lotteryStrategyRepository.pageStrategyInfo(pageDTO);
    }

    @Override
    public DrawReq getActStrategy(Integer strategyId) {
        final LotteryStrategyVO strategyById = LotteryStrategyFactory.getStrategyById(strategyId);
        if(strategyById==null){
            return null;
        }
        DrawReq req=new DrawReq();
        req.setStrategyType(new Pair<Integer,Integer>(strategyById.getSuitNum(),strategyById.getStrategyMode()));
        return req;
    }
}
